﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class CustomerJiageHis : BasePage
    {
        private ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_chanpinjiageHisList"] = null;
                Session["SortDirection_chanpinjiageHisList"] = null;
                Session["txtCPCode"] = null;
                Session["txtCPqixiri"] = null;
                Session["txtCPdaoqiri"] = null;
            }          
            
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvchanpinjiageHis.PageIndex = index;

                Session["chanpinjiageHisListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_chanpinjiageHisList"] != null)
            {
                this.gvchanpinjiageHis.Attributes["SortExpression_chanpinjiageHisList"] = Session["SortExpression_chanpinjiageHisList"].ToString();
                this.gvchanpinjiageHis.Attributes["SortDirection_chanpinjiageHisList"] = Session["SortDirection_chanpinjiageHisList"].ToString();
                setSortColumnStyle(this.gvchanpinjiageHis, Session["SortExpression_chanpinjiageHisList"].ToString(), Session["SortDirection_chanpinjiageHisList"].ToString());
            }
            //setSortColumnStyle(gvchanpinjiageHis, this.gvchanpinjiageHis.Attributes["SortExpression_chanpinjiageHisList"].ToString(),
            //     this.gvchanpinjiageHis.Attributes["SortDirection_chanpinjiageHisList"].ToString());
            if (!IsPostBack)
            {
                if (Request.QueryString["code"] != null)
                {
                    txtCPCode.Text = Request.QueryString["code"];
                }
                if (Request.QueryString["qixiri"] != null)
                {
                    txtCPqixiri.Text = Request.QueryString["qixiri"];
                }
                if (Request.QueryString["daoqiri"] != null)
                {
                    txtCPdaoqiri.Text = Request.QueryString["daoqiri"];
                }

                ViewState["chanpinjiageHisAll"] = dao.Get_CustomerChanPinJiage_His(
                    SpecialCharHandler.HtmlEncode(txtCPCode.Text),
                    SpecialCharHandler.HtmlEncode(txtCPqixiri.Text),
                     SpecialCharHandler.HtmlEncode(txtCPdaoqiri.Text));

                SearchchanpinjiageHisBindData();
            }
        }
        private void setSessionData()
        {
            //检索条件记录
            if (Session["txtCPCode"] != null)
            {
                txtCPCode.Text = SpecialCharHandler.HtmlDiscode(Session["txtCPCode"].ToString());
            }
            if (Session["txtCPqixiri"] != null)
            {
                txtCPqixiri.Text = SpecialCharHandler.HtmlDiscode(Session["txtCPqixiri"].ToString());
            }
            if (Session["txtCPdaoqiri"] != null)
            {
                txtCPdaoqiri.Text = SpecialCharHandler.HtmlDiscode(Session["txtCPdaoqiri"].ToString());
            }
            if (Session["chanpinjiageHisListIndex"] != null)
            {
                gvchanpinjiageHis.PageIndex = (int)Session["chanpinjiageHisListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvchanpinjiageHis_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                //((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditchanpinjiageHis.aspx?chanpinjiageHisID="
                //    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;") + "&mid=" + mid;

                //e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                //string jianjie = SpecialCharHandler.HtmlDiscode(e.Row.Cells[2].Text);
                //e.Row.Cells[2].ToolTip = jianjie;
                //if (jianjie.Length > 15)
                //{
                //    jianjie = jianjie.Substring(0, 15) + "...";
                //}
                //e.Row.Cells[2].Text = jianjie;
                //e.Row.Cells[3].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[3].Text);
                //e.Row.Cells[4].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[4].Text);

            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["chanpinjiageHisAll"] = dao.Get_CustomerChanPinJiage_His(
                    SpecialCharHandler.HtmlEncode(txtCPCode.Text),
                    SpecialCharHandler.HtmlEncode(txtCPqixiri.Text),
                     SpecialCharHandler.HtmlEncode(txtCPdaoqiri.Text));
            //清空分页状态
            Session["SortExpression_chanpinjiageHisList"] = null;
            Session["SortDirection_chanpinjiageHisList"] = null;

            //检索条件记录
            Session["txtCPCode"] = SpecialCharHandler.HtmlEncode(txtCPCode.Text);
            Session["txtCPqixiri"] = SpecialCharHandler.HtmlEncode(txtCPqixiri.Text);
            Session["txtCPdaoqiri"] = SpecialCharHandler.HtmlEncode(txtCPdaoqiri.Text);
            SearchchanpinjiageHisBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvchanpinjiageHis_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvchanpinjiageHis.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["chanpinjiageHisListIndex"] = pageIndex;
            SearchchanpinjiageHisBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvchanpinjiageHis_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_chanpinjiageHisList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_chanpinjiageHisList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_chanpinjiageHisList == this.gvchanpinjiageHis.Attributes["SortExpression_chanpinjiageHisList"])
            {
                //获得下一次的排序状态
                SortDirection_chanpinjiageHisList = (this.gvchanpinjiageHis.Attributes["SortDirection_chanpinjiageHisList"].ToString() == SortDirection_chanpinjiageHisList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvchanpinjiageHis.Attributes["SortExpression_chanpinjiageHisList"] = SortExpression_chanpinjiageHisList;
            this.gvchanpinjiageHis.Attributes["SortDirection_chanpinjiageHisList"] = SortDirection_chanpinjiageHisList;
            //记录分页状态
            Session["SortExpression_chanpinjiageHisList"] = SortExpression_chanpinjiageHisList;
            Session["SortDirection_chanpinjiageHisList"] = SortDirection_chanpinjiageHisList;

            setSortColumnStyle(this.gvchanpinjiageHis, SortExpression_chanpinjiageHisList, SortDirection_chanpinjiageHisList);
            this.SearchchanpinjiageHisBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchchanpinjiageHisBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_chanpinjiageHisList = this.gvchanpinjiageHis.Attributes["SortExpression_chanpinjiageHisList"];
            string SortDirection_chanpinjiageHisList = this.gvchanpinjiageHis.Attributes["SortDirection_chanpinjiageHisList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["chanpinjiageHisAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_chanpinjiageHisList)) && (!string.IsNullOrEmpty(SortDirection_chanpinjiageHisList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_chanpinjiageHisList, SortDirection_chanpinjiageHisList);
            }
            // GridView绑定并显示数据
            this.gvchanpinjiageHis.DataSource = dtBind;
            this.gvchanpinjiageHis.DataBind();

            pageCount = gvchanpinjiageHis.PageCount;
            pageIndex = gvchanpinjiageHis.PageIndex;

        }
    }
}